import fs from "fs";

const initFolder = async (folder) => {
    if (fs.existsSync(folder)) {
        return;
    }
    if (folder.endsWith('/')) {
        folder = folder.slice(0, folder.length - 1);
    }
    const folderArr = folder.split('/').reduce((t, v) => {
        if (t.length) {
            t.push(t[t.length - 1] + '/' + v)
        } else {
            t.push(v)
        }
        return t;
    }, []);
    const index = folderArr.findIndex(v => !fs.existsSync(v));
    const mkdirArr = folderArr.slice(index);
    for (let v of mkdirArr) {
        await fs.mkdirSync(v);
    }
}

export default initFolder;